/*ui_mask编辑*/
(function ($) {
    $.fn.yqTreeEdit = function (opts, param) {
        if (typeof opts == 'string') {
            return $.fn.yqTreeEdit.methods[opts](this, param);
        }

        var $this = this;
        var opt = $.extend({}, $.fn.yqTreeEdit.defaults, opts);
        if ($this.data("yqtype") == "yqTreeEdit") {
            var $uniqueID = $this.attr("ID");
            destroy($this);
            $this = $("#" + $uniqueID);
        }
        else {
            var $uniqueID = $this.attr("ID");
            if ($uniqueID === undefined || $uniqueID === null || $uniqueID == "") {
                $uniqueID = $.getUniqueID();
                $this.attr("ID", $uniqueID);
            }
        }

        if (opt.editModeW === undefined && $this.width() > 0) {
            opt.editModeW = $this.width();
        }
        else {
            if (opt.editModeW === undefined) {
                opt.editModeW = 400;
            }
            $this.width(opt.editModeW);
        }

        if (opt.editModeH === undefined && $this.height() > 0) {
            opt.editModeH = $this.height();
        }
        else {
            if (opt.editModeH === undefined) {
                opt.editModeH = 300;
            }
            $this.height(opt.editModeH);
        }

        opt.fk_rows = [];

        opt.on_load = false;
        opt.isMove = false;

        //变量信息
        $this.data("treeedit_opt", opt);
        $this.data("yqtype", "yqTreeEdit");

        init_pg_before($this);
    };

    $.fn.yqTreeEdit.defaults = {
        ajaxUrl: "/_db.html",
        displayMember: "name",          //显示字段
        titles: [],  //数据列配置对象
        data: [],
        // editModeW: 500, //模态编辑器宽度 格式为 800（数字）
        // editModeH: 300, //模态编辑器高度 格式为 600（数字）
        isEdit: true,
        isAdd: true,
        isDel: true,
        beforeBindingTitle: function (titles, callback) {
            callback(titles);
        }, //标题绑定前
        afterBindingTitle: function () { },   //标题绑定后
        beforeAdd: function (rows, row, callback) {
            callback(row);
        },
        endEditing: function (index, row) {   //ok.....

        },
    };

    $.fn.yqTreeEdit.methods = {
        options: function (jq) {
            return options($(jq[0]));
        },
        setTitles: function (jq, titles) {
            options(jq).titles = titles;
        },
        clear: function (jq, data) {
            setData($(jq[0]), []);
        },
        reLoad: function (jq) {
            reLoad(jq);
        },
        setData: function (jq, data) {
            setData($(jq[0]), data);
        },
        getData: function (jq) {
            return getData($(jq[0]));
        },
        isEdit: function (jq, tf) {
            isEdit($(jq[0]), tf);
        },
        destroy: function (jq) {
            destroy($(jq[0]));
        },
        resize: function (jq, params) {
            resize($(jq[0]), params)
        },
        setField: function (jq, para) {
            if (para === undefined) {
                return;
            }
            if (para.name === undefined) {
                return;
            }
            if (para.value === undefined) {
                return;
            }

            setfield(jq, para.name, para.value)
        }
    };

    function options($object) {
        return $object.data("treeedit_opt");
    }

    function resize($object, params) {
        var opt = options($object);
        if (!(params === undefined)) {
            if (!(params.width === undefined)) {
                opt.width = params.width;
            }
            if (!(params.height === undefined)) {
                opt.height = params.height;
            }
        }

        $object.treegrid("resize", {
            width: opt.width,
            height: opt.height
        });
    }

    function destroy($object) {
        var opt = options($object);
        var $uniqueID = $object.attr("id");
        if (typeof opt.$menu != "undefined") {
            opt.$menu.menu("destroy");
        }
        if (typeof opt.$toolbar != "undefined") {
            opt.$toolbar.remove();
        }

        var $div = $object.closest("div.datagrid");
        $div.before($object);
        $object.show();
        $div.remove();

        $object.removeClass("datagrid-f");
        $object.css({ width: opt.editModeW, height: opt.editModeH });

        $object.removeData("treeedit_opt");
        $object.removeData("yqtype");

        var $table = $("<table></table>");
        $table.css({ width: opt.editModeW, height: opt.editModeH });
        $object.before($table);
        $object.remove();
        $table.attr("id", $uniqueID);
    }

    function init_pg_before($object) {
        var opt = options($object);
        opt.titles = $.yqFmUiTitle(opt.titles);
        opt.beforeBindingTitle.call($object, opt.titles,
            function (titles) {
                opt.titles = titles;
                setForienKeys($object, opt.data,
                    function () {
                        init_pg($object);
                    });
            }
        );
    }

    function init_pg($object) {
        var opt = options($object);
        var $uniqueID = $object.attr("id");

        opt.$toolbar = $("<div></div>");
        if (opt.isAdd) {
            opt.$toolbar.append('<a id="' + "toolbar_" + $uniqueID + "_addroot" + '"></a>');
            opt.$toolbar.append('<a id="' + "toolbar_" + $uniqueID + "_add" + '"></a>');
            opt.$toolbar.append('<a id="' + "toolbar_" + $uniqueID + "_append" + '"></a>');
            opt.$toolbar.append('<a id="' + "toolbar_" + $uniqueID + "_copy" + '"></a>');
            if (opt.isDel) {
                opt.$toolbar.append('<a class="pagination-btn-separator" style="float: none;" href="javascript:void(0);"></a>');
            }
        }
        if (opt.isDel) {
            opt.$toolbar.append('<a id="' + "toolbar_" + $uniqueID + "_del" + '"></a>')
        }

        if (opt.$toolbar.children().length > 0) {
            opt.$menu = $(`<div style="width:120px;">
                            <div id="menu_` + $uniqueID + `_addroot" data-options="name:'addroot',iconCls:'yqwin-btn-icon-sub fa fa-home'">添加根</div>
                            <div id="menu_` + $uniqueID + `_add" data-options="name:'add',iconCls:'yqwin-btn-icon-sub fa fa-plus-square'">添加</div>
                            <div id="menu_` + $uniqueID + `_append" data-options="name:'append',iconCls:'yqwin-btn-icon-sub fa fa-indent'">添加(同级)</div>
                            <div id="menu_` + $uniqueID + `_copy" data-options="name:'copy',iconCls:'yqwin-btn-icon-sub fa fa-files-o'">拷贝</div>
                            <div class="menu-sep"></div>
                            <div id="menu_` + $uniqueID + `_del", data-options="name:'del',iconCls:'yqwin-btn-icon-sub fa fa-trash'">删除</div>
                        </div>`);

            opt.$menu.menu({
                onClick: function (item) {
                    if (item.name == "addroot") {
                        addRoot($object);
                    }
                    else if (item.name == "add") {
                        add($object);
                    }
                    else if (item.name == "append") {
                        append($object);
                    }
                    else if (item.name == "copy") {
                        copy($object);
                    }
                    else if (item.name == "del") {
                        deleteRun($object);
                    }
                }
            });
        }

        var titles_ = [];
        for (let i = 0; i < opt.titles.length; i++) {
            if (opt.titles[i].name == opt.displayMember) {
                titles_.unshift(opt.titles[i]);
            }
            else {
                titles_.push(opt.titles[i]);
            }
        }
        opt.titles = titles_;

        var columns = $.yqDgColumns.call($object, opt.titles, opt.isEdit);
        var res_nodes = $.yqTreeloadInit(opt.data);
        id_ = res_nodes.id_;

        var dg_opt = {
            width: opt.editModeW,
            height: opt.editModeH,

            toolbar: opt.$toolbar,
            currentId_: id_,
            idField: 'id_',
            treeField: opt.displayMember,
            columns: columns,
            data: res_nodes.data,
            singleSelect: true,

            onContextMenu: function (e, row) {
                if (opt.isMove || (!opt.isEdit)) {
                    return;
                }

                var node = $(this).treegrid("getSelected");
                if (opt.isAdd) {
                    if (node == null) {
                        opt.$menu.menu("disableItem", $("#menu_" + $uniqueID + "_append"));
                        opt.$menu.menu("disableItem", $("#menu_" + $uniqueID + "_add"));
                        opt.$menu.menu("disableItem", $("#menu_" + $uniqueID + "_copy"));
                    }
                    else {
                        opt.$menu.menu("enableItem", $("#menu_" + $uniqueID + "_append"));
                        opt.$menu.menu("enableItem", $("#menu_" + $uniqueID + "_add"));
                        opt.$menu.menu("enableItem", $("#menu_" + $uniqueID + "_copy"));
                    }
                }

                if (opt.isDel) {
                    if (node == null) {
                        opt.$menu.menu("disableItem", $("#menu_" + $uniqueID + "_del"));
                    }
                    else {
                        opt.$menu.menu("enableItem", $("#menu_" + $uniqueID + "_del"));
                    }
                }

                e.preventDefault();
                opt.$menu.menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            },
            onSelect: function (row) {
                var node = $(this).treegrid('getSelected');
                upBtnState($object, node);

                if (opt.isEdit && (!opt.isMove)) {
                    setEditNode($(this), node);
                }
            },
            onLoadSuccess: function (row, data) {
                upBtnState($object, null);
                //tgEnableDnd($(this));

                if (!opt.on_load) {
                    opt.on_load = true;
                    opt.afterBindingTitle.call($object);
                }
            },
        };

        if (opt.$toolbar.children().length > 0) {
            opt.$toolbar.append('<a id="' + "toolbar_" + $uniqueID + "_move" + '"></a>')
            $object.append(opt.$toolbar);
            dg_opt.toolbar = opt.$toolbar;

            $("#toolbar_" + $uniqueID + "_addroot").linkbutton({
                text: '添加根',
                iconCls: 'yqwin-btn-icon fa fa-home',
                plain: true,
                onClick: function () {
                    addRoot($object);
                }
            });
            $("#toolbar_" + $uniqueID + "_add").linkbutton({
                text: '添加',
                iconCls: 'yqwin-btn-icon fa fa-plus-square',
                plain: true,
                onClick: function () {
                    add($object);
                }
            });
            $("#toolbar_" + $uniqueID + "_append").linkbutton({
                text: '添加(同级)',
                iconCls: 'yqwin-btn-icon fa fa-indent',
                plain: true,
                onClick: function () {
                    append($object);
                }
            });
            $("#toolbar_" + $uniqueID + "_copy").linkbutton({
                text: '拷贝',
                iconCls: 'yqwin-btn-icon fa fa-files-o',
                plain: true,
                onClick: function () {
                    copy($object);
                }
            });

            $("#toolbar_" + $uniqueID + "_del").linkbutton({
                text: '删除',
                iconCls: 'yqwin-btn-icon fa fa-trash',
                plain: true,
                onClick: function () {
                    deleteRun($object);
                }
            });

            $("#toolbar_" + $uniqueID + "_move").data("p_object", $object)
            $("#toolbar_" + $uniqueID + "_move").switchbutton({
                width: 55,
                height: 23,
                checked: false,
                handleText: '<span class="yqwin-btn-icon fa fa-sort" aria-hidden="true"></span>',
                onText: "on",
                offText: "off",
                onChange: function (checked) {
                    opt.isMove = checked;
                    if (checked) {
                        if (endEditing($object)) {
                            tgEnableDnd($object);
                        }
                    } else {
                        tgDisableDnd($object);
                    }

                    var node = $object.treegrid("getSelected");
                    upBtnState($object, node);
                }
            })
        }

        $object.treegrid(dg_opt);
    }

    function upBtnState($object, node) {
        var opt = options($object);
        var $uniqueID = $object.attr("id");
        if (opt.isEdit) {
            $("#toolbar_" + $uniqueID + "_move").switchbutton("enable");
        } else {
            $("#toolbar_" + $uniqueID + "_move").switchbutton("disable");
        }

        opt.$menu.menu("disableItem", $("#menu_" + $uniqueID + "_del"));
        if (opt.isEdit && (!opt.isMove)) {
            if (node === null) {
                $("#toolbar_" + $uniqueID + "_addroot").linkbutton("enable");
                $("#toolbar_" + $uniqueID + "_add").linkbutton("disable");
                $("#toolbar_" + $uniqueID + "_append").linkbutton("disable");
                $("#toolbar_" + $uniqueID + "_copy").linkbutton("disable");
                $("#toolbar_" + $uniqueID + "_del").linkbutton("disable");

                opt.$menu.menu("enableItem", $("#menu_" + $uniqueID + "_addroot"));
                opt.$menu.menu("disableItem", $("#menu_" + $uniqueID + "_add"));
                opt.$menu.menu("disableItem", $("#menu_" + $uniqueID + "_append"));
                opt.$menu.menu("disableItem", $("#menu_" + $uniqueID + "_copy"));
                opt.$menu.menu("disableItem", $("#menu_" + $uniqueID + "_del"));
            }
            else {
                $("#toolbar_" + $uniqueID + "_addroot").linkbutton("enable");
                $("#toolbar_" + $uniqueID + "_add").linkbutton("enable");
                $("#toolbar_" + $uniqueID + "_append").linkbutton("enable");
                $("#toolbar_" + $uniqueID + "_copy").linkbutton("enable");
                $("#toolbar_" + $uniqueID + "_del").linkbutton("enable");

                opt.$menu.menu("enableItem", $("#menu_" + $uniqueID + "_addroot"));
                opt.$menu.menu("enableItem", $("#menu_" + $uniqueID + "_add"));
                opt.$menu.menu("enableItem", $("#menu_" + $uniqueID + "_append"));
                opt.$menu.menu("enableItem", $("#menu_" + $uniqueID + "_copy"));
                opt.$menu.menu("enableItem", $("#menu_" + $uniqueID + "_del"));
            }
        }
        else {
            $("#toolbar_" + $uniqueID + "_addroot").linkbutton("disable");
            $("#toolbar_" + $uniqueID + "_add").linkbutton("disable");
            $("#toolbar_" + $uniqueID + "_append").linkbutton("disable");
            $("#toolbar_" + $uniqueID + "_copy").linkbutton("disable");
            $("#toolbar_" + $uniqueID + "_del").linkbutton("disable");

            opt.$menu.menu("disableItem", $("#menu_" + $uniqueID + "_addroot"));
            opt.$menu.menu("disableItem", $("#menu_" + $uniqueID + "_add"));
            opt.$menu.menu("disableItem", $("#menu_" + $uniqueID + "_append"));
            opt.$menu.menu("disableItem", $("#menu_" + $uniqueID + "_copy"));
            opt.$menu.menu("disableItem", $("#menu_" + $uniqueID + "_del"));
        }
    }

    function reLoad($object) {
        var opt = options($object);
        var titles_ = [];
        for (let i = 0; i < opt.titles.length; i++) {
            if (opt.titles[i].name == opt.displayMember) {
                titles_.unshift(opt.titles[i]);
            }
            else {
                titles_.push(opt.titles[i]);
            }
        }
        opt.titles = titles_;
        var columns = $.yqDgColumns.call($object, opt.titles, opt.isEdit);
        setForienKeys($object, opt.data,
            function () {
                var res_nodes = $.yqTreeloadInit(opt.data);
                id_ = res_nodes.id_;

                $object.treegrid({
                    currentId_: id_,
                    idField: 'id_',
                    treeField: opt.displayMember,
                    columns: columns,
                    data: res_nodes.data
                });
            });
    }

    function setEditNode($object, node) {
        if (node === null) {
            return;
        }
        var opt_ = $object.treegrid('options');
        if (node) {
            if (endEditing($object)) {
                if (opt_.editIndex === undefined) {
                    $object.treegrid('beginEdit', node.id_);
                    opt_.editIndex = node.id_;
                } else if (opt_.editIndex != node.id_) {
                    $object.treegrid('cancelEdit', opt_.editIndex);
                    $object.treegrid('beginEdit', node.id_);

                    opt_.editIndex = node.id_;
                }
            }
        }
    }

    function endEditing($object) {
        var opt_ = $object.treegrid('options');
        if (opt_.editIndex === undefined) { return true }
        var s_node = $object.treegrid('find', opt_.editIndex);
        if (s_node) {
            $object.treegrid('endEdit', opt_.editIndex);
            opt_.editIndex = undefined;
            return true;
        } else {
            return false;
        }
    }

    function addRoot($object) {
        if (!endEditing($object)) {
            return;
        }
        var opt_ = $object.treegrid('options');
        opt_.currentId_++;

        var row = {
            id_: opt_.currentId_
        };
        row[opt_.displayMember] = "";

        var data = $object.treegrid('getData');
        data = $.yqTreeSaveInit(data);

        var opt = options($object);
        opt.beforeAdd.call($object, data, row,
            function (row2) {
                $object.treegrid('append', {
                    data: [row2]
                });

                var node = $object.treegrid('find', opt_.currentId_);
                setEditNode($object, node);
            });
    }

    function add($object) {
        var opt_ = $object.treegrid('options');
        if (opt_.editIndex === undefined) {
            return;
        }

        var parent_id = opt_.editIndex;
        if (!endEditing($object)) {
            return;
        }

        var opt_ = $object.treegrid('options');
        opt_.currentId_++;

        var row = {
            id_: opt_.currentId_
        };
        row[opt_.displayMember] = "";

        var data = $object.treegrid('getData');
        data = $.yqTreeSaveInit(data);

        var opt = options($object);
        opt.beforeAdd.call($object, data, row,
            function (row2) {
                $object.treegrid('append', {
                    parent: parent_id,
                    data: [row2]
                });

                var node = $object.treegrid('find', opt_.currentId_);
                setEditNode($object, node);
            });
    }

    function append($object) {
        var opt_ = $object.treegrid('options');
        if (opt_.editIndex === undefined) {
            return;
        }

        var parent_id = opt_.editIndex;
        if (!endEditing($object)) {
            return;
        }

        var opt_ = $object.treegrid('options');
        opt_.currentId_++;

        var row = {
            id_: opt_.currentId_
        };
        row[opt_.displayMember] = "";

        var data = $object.treegrid('getData');
        data = $.yqTreeSaveInit(data);

        var opt = options($object);
        opt.beforeAdd.call($object, data, row,
            function (row2) {
                var parent_node_ = $object.treegrid('find', parent_id);
                console.log("parent_node_", parent_node_);
                $object.treegrid('append', {
                    parent: parent_node_._parentId,
                    data: [row2]
                });

                var node = $object.treegrid('find', opt_.currentId_);
                setEditNode($object, node);
            });
    }

    function copy($object) {
        var opt_ = $object.treegrid('options');
        if (opt_.editIndex === undefined) {
            return;
        }

        var parent_id = opt_.editIndex;
        if (!endEditing($object)) {
            return;
        }

        var node_ = $object.treegrid('find', parent_id);
        if (node_ === null) {
            return;
        }
        var row = JSON.parse(JSON.stringify(node_));
        delete (row["children"]);
        delete (row["checked"]);
        delete (row["checkState"]);
        delete (row["state"]);

        var opt_ = $object.treegrid('options');
        opt_.currentId_++;
        row.id_ = opt_.currentId_;

        var data = $object.treegrid('getData');
        data = $.yqTreeSaveInit(data);

        var opt = options($object);
        opt.beforeAdd.call($object, data, row,
            function (row2) {
                if (row2._parentId === undefined) {
                    $object.treegrid('append', {
                        data: [row2]
                    });
                } else {
                    $object.treegrid('append', {
                        parent: row2._parentId,
                        data: [row2]
                    });
                }
                var node = $object.treegrid('find', opt_.currentId_);
                setEditNode($object, node);
            });
    }

    function deleteRun($object) {
        var opt_ = $object.treegrid('options');
        if (opt_.editIndex === undefined) {
            return;
        }

        var id_ = opt_.editIndex;
        if (!endEditing($object)) {
            return;
        }

        $object.treegrid('remove', id_);
    }

    function tgEnableDnd($object) {
        var opt_ = $object.treegrid('options');
        opt_.drapselect = {
            node_: null,
            body_: null
        };

        var nodes = $object.treegrid('getPanel').find('tr[node-id]');
        nodes.find('span.tree-hit').bind('mousedown.treegrid', function () {
            return false;
        });
        nodes.draggable({
            disabled: false,
            revert: true,
            cursor: 'pointer',
            proxy: function (source) {
                var p = $('<div class="tree-node-proxy tree-dnd-no"></div>').appendTo('body');
                p.html($(source).find('.tree-title').html());
                p.hide();
                return p;
            },
            deltaX: 15,
            deltaY: 15,
            onBeforeDrag: function () {
                $(this).next('tr.treegrid-tr-tree').find('tr[node-id]').droppable({ accept: 'no-accept' });
            },
            onStartDrag: function () {
                $(this).draggable('proxy').css({
                    left: -10000,
                    top: -10000
                });
            },
            onDrag: function (e) {
                try {
                    $(this).draggable('proxy').show();
                    this.pageY = e.pageY;
                } catch (error) {
                    console.log("error", error);
                }
            },
            onStopDrag: function () {
                $(this).next('tr.treegrid-tr-tree').find('tr[node-id]').droppable({ accept: 'tr[node-id]' });
            }
        }).droppable({
            accept: 'tr[node-id]',
            onDragOver: function (e, source) {
                var pageY = source.pageY;
                var top = $(this).offset().top;
                var bottom = top + $(this).outerHeight();
                $(source).draggable('proxy').removeClass('tree-dnd-no').addClass('tree-dnd-yes');
                $(this).removeClass('row-append row-top row-bottom');
                if (pageY > top + (bottom - top) / 2) {
                    if (bottom - pageY < 5) {
                        $(this).addClass('row-bottom');
                    } else {
                        $(this).addClass('row-append');
                    }
                } else {
                    if (pageY - top < 5) {
                        $(this).addClass('row-top');
                    } else {
                        $(this).addClass('row-append');
                    }
                }

                opt_.drapselect.node_ = $(this);
            },
            onDragLeave: function (e, source) {
                $(source).draggable('proxy').removeClass('tree-dnd-yes').addClass('tree-dnd-no');
                $(this).removeClass('row-append row-top row-bottom');

                opt_.drapselect.node_ = null;
            },
            onDrop: function (e, source) {
                return;
            }
        });

        var $body = $object.treegrid('getPanel').find('.datagrid-view2 .datagrid-body');
        $body.droppable({
            accept: 'tr[node-id]',
            onDragOver: function (e, source) {
                $(source).draggable('proxy').removeClass('tree-dnd-no').addClass('tree-dnd-yes');

                opt_.drapselect.body_ = $(this);
            },
            onDragLeave: function (e, source) {
                $(source).draggable('proxy').removeClass('tree-dnd-yes').addClass('tree-dnd-no');

                opt_.drapselect.body_ = null;
            },
            onDrop: function (e, source) {
                var src = $object.treegrid('find', $(source).attr('node-id'));
                var srcnode = $object.treegrid('pop', $(source).attr('node-id'));

                if (!(opt_.drapselect.body_ === null) &&
                    !(opt_.drapselect.node_ === null)) {
                    // var action;
                    // if (opt_.drapselect.node_.hasClass('row-append')) {
                    //     action = 'append';
                    // } else {
                    //     action = 'insert';
                    // }
                    opt_.drapselect.node_.removeClass('row-append row-top row-bottom');
                    var dest = $object.treegrid('find', opt_.drapselect.node_.attr('node-id'));

                    if (src._parentId == dest._parentId) {
                        $object.treegrid('insert', {
                            before: dest.id_,
                            data: srcnode
                        });
                    } else {
                        $object.treegrid('append', {
                            parent: dest.id_,
                            data: [srcnode]
                        });
                    }
                } else {
                    $object.treegrid('append', {
                        data: [srcnode]
                    });
                }

                tgDisableDnd($object)
                tgEnableDnd($object);
            }
        });
    }

    function tgDisableDnd($object) {
        var nodes = $object.treegrid('getPanel').find('tr[node-id]');
        nodes.find('span.tree-hit').bind('mousedown.treegrid', function () {
            return false;
        });
        nodes.draggable("disable");

        var $body = $object.treegrid('getPanel').find('.datagrid-view2 .datagrid-body');
        $body.draggable("disable");
    }

    function setData($object, data) {
        var opt = options($object);
        if (data === undefined || data === null || data == "") {
            data = [];
        }
        if (typeof rows === "string") {
            data = JSON.parse(data);
        }
        opt.data = data;
        reLoad($object);
    }

    //外键处理
    function setForienKeys($object, rows, callback) {
        var opt = options($object);
        var opts = {
            "ajaxUrl": opt.ajaxUrl,
            "rows": rows,
            "titles": opt.titles,
            "fk_rows": opt.fk_rows
        };

        if (typeof opt._appno != undefined) {
            opts.ctrlid = opt._ctrlid;
            opts.appno = opt._appno;
            opts.menuno = opt._menuno;
        }

        $.yqGetForeignkeys(opts,
            function (fk_rows) {
                opt.fk_rows = fk_rows;
                callback();
            });
    }

    function setfield($object, field, val) {
        var opt = options($object);

        var index = $object.treegrid("options").editIndex;
        if (index === undefined) {
            return;
        }

        if (val === undefined) {
            val = null;
        }

        var $editor = $object.treegrid("getEditor",
            {
                index: index,
                field: field
            });

        if ($editor === null) {
            var row = $object.treegrid("getSelected");
            if (val === null) {
                delete row[field];
            } else {
                row[field] = val;
            }
            //console.log($object.treegrid("options"), val, row);
            var $td = $object.closest('div.datagrid-view')
                .find('.datagrid-view2')
                .find('.datagrid-btable')
                .find('tr[node-id="' + row.id_ + '"]')
                .find('td[field="' + field + '"]');
            if ($td.length > 0) {
                $td.find('div').html(val);
            }
        } else {
            $.yqDgEditorSetValue($editor.target, val)
        }
    }


    function getData($object) {
        var opt = options($object);
        if (endEditing($object)) {
            var data = $object.treegrid('getData');
            data = $.yqTreeSaveInit(data);
            if (data.length == 0) {
                data = [];
            }
            else {
                data = $.yqFmToDatas(data, opt.titles, opt.fk_rows);
            }
            opt.data = data;
            return data;
        }
        else {
            return opt.data;
        }
    }

    function isEdit($object, tf) {
        var opt = options($object);
        opt.isEdit = tf;
        if (endEditing($object)) {
            var data = $object.treegrid('getData');
            data = $.yqTreeSaveInit(data);
            opt.data = data;
            reLoad($object);
        }
    }
})(jQuery);

